package com.scuec.trainingmanagementsys.controller;
import com.scuec.trainingmanagementsys.entity.Assessment;
import com.scuec.trainingmanagementsys.service.AssessmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/assessments")
@CrossOrigin
public class AssessmentController {
    @Autowired
    private AssessmentService assessmentService;

    // 增加评估记录
    @PostMapping("/assinsert")
    public ResponseEntity<Assessment> addAssessment(@RequestBody Assessment assessment) {
        int result = assessmentService.addAssessment(assessment);
        return result > 0 ? ResponseEntity.ok(assessment) : ResponseEntity.badRequest().build();
    }

    // 删除评估记录
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteAssessment(@PathVariable("id") Integer assessmentID) {
        int result = assessmentService.deleteAssessmentById(assessmentID);
        return result > 0 ? ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
    }

    // 根据ID查找评估记录
    @GetMapping("/{id}")
    public ResponseEntity<Assessment> findAssessmentById(@PathVariable("id") Integer assessmentID) {
        Assessment assessment = assessmentService.findAssessmentById(assessmentID);
        return assessment != null ? ResponseEntity.ok(assessment) : ResponseEntity.notFound().build();
    }

    // 查找所有评估记录
    @GetMapping("/asslist")
    public ResponseEntity<List<Assessment>> findAllAssessments() {
        List<Assessment> assessments = assessmentService.findAllAssessments();
        return ResponseEntity.ok(assessments);
    }

    // 根据教师ID查找评估记录
    @GetMapping("/teacher/{teacherId}")
    public ResponseEntity<List<Assessment>> findAssessmentsByTeacherId(@PathVariable("teacherId") Integer teacherID) {
        List<Assessment> assessments = assessmentService.findAssessmentsByTeacherId(teacherID);
        return ResponseEntity.ok(assessments);
    }

    // 修改评估记录（新增）
    @PutMapping("/{id}")
    public ResponseEntity<Assessment> updateAssessment(@PathVariable("id") Integer assessmentID,
                                                       @RequestBody Assessment assessment) {
        if (!assessmentID.equals(assessment.getAssessmentID())) {
            return ResponseEntity.badRequest().build();
        }
        int result = assessmentService.updateAssessment(assessment);
        return result > 0 ? ResponseEntity.ok(assessment) : ResponseEntity.notFound().build();
    }
    @GetMapping("/daily-report/{dailyReportID}")
    public ResponseEntity<List<Assessment>> findAssessmentsByDailyReportID(@PathVariable("dailyReportID") Integer dailyReportID) {
        List<Assessment> assessments = assessmentService.findAssessmentsByDailyReportID(dailyReportID);
        return ResponseEntity.ok(assessments);
    }

    @GetMapping("/daily-report/details/{dailyReportID}")
    public ResponseEntity<List<Map<String, Object>>> findAssessmentsByDailyReportIDWithDetails(@PathVariable("dailyReportID") Integer dailyReportID) {
        List<Map<String, Object>> assessments = assessmentService.findAssessmentsByDailyReportIDWithDetails(dailyReportID);
        return ResponseEntity.ok(assessments);
    }
}